package com.jubaozan.service.platform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.JsonObject;
import com.jubaozan.c3.framework.constants.Constants;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.c3.framework.utils.PageHolder;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.c3.framework.utils.ResultStatus;
import com.jubaozan.dao.youme.domain.Store;
import com.jubaozan.service.platform.decorator.ConsumeActivityDecorator;
import com.jubaozan.service.platform.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ConsumeActivityController
 * @Description 消费者活动相关接口Controller
 * @Author TanRuixiang
 * @Date 2020/9/28 10:54
 * @Version 1.0
 **/
@Api(tags = {"消费者活动相关接口"}, value = "消费者活动相关接口")
@RestController
@RequestMapping("/consume/activity")
@Validated
@Slf4j
public class ConsumeActivityController {


    private final ConsumeActivityDecorator consumeActivityDecorator;


    public ConsumeActivityController(ConsumeActivityDecorator consumeActivityDecorator) {
        this.consumeActivityDecorator = consumeActivityDecorator;
    }


    /**
     * @param storeId: 平台ID
     * @return
     * @Author TanRuixiang
     * @Description 获取个体平台信息
     * @Date 2020年09月28日 10:58
     **/
    @ApiOperation(value = "获取个体平台信息")
    @GetMapping("/store")
    public ResultHolder<Store> getStoreInfo(@RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId) {
        return ResultHolder.success(consumeActivityDecorator.getStoreInfo(userId));
    }

    /**
     * @param storeId: 平台ID
     * @param status:  1.正在进行的,2.已经结束的,3.还未开始的
     * @return
     * @Author TanRuixiang
     * @Description 获取个体平台的活动列表(正在进行的, 已经结束的, 还未开始的)
     * @Date 2020年09月28日 11:00
     **/
    @ApiOperation(value = "获取个体平台的活动信息")
    @GetMapping("")
    public ResultHolder<ActivityListVO> getActivityInfor(@RequestHeader(Constants.KEY_X_C3_STOREID) Long storeId, Integer status) {
        if (ObjectUtil.isEmpty(status)) {
            return ResultHolder.error(ResultStatus.SERVICE_ERROR, "活动状态不能为空");
        }
        return ResultHolder.success(consumeActivityDecorator.getActivityInfor(storeId, status));
    }

    @ApiOperation(value = "获取个体平台活动的商品列表")
    @GetMapping("/goods")
    public ResultHolder<IPage<StoreGoodsVO>> getGoodsListById(@RequestHeader(Constants.KEY_X_C3_STOREID) Long storeId,
                                                              @RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId,Long activityId, PageHolder pageHolder){
        IPage<StoreGoodsVO> iPage = consumeActivityDecorator.getGoodsListById(storeId,activityId,userId,pageHolder);
        logger.info("返回给前端的结果集:{}", JSONObject.toJSONString(iPage));
        return ResultHolder.success(iPage);
    }


    @ApiOperation(value = "获取活动正在看的用户信息")
    @GetMapping("/looking/{activityId}")
    public ResultHolder<IPage<UserInfoVO>> getLookingById(@RequestHeader(Constants.KEY_X_C3_STOREID) Long storeId, Long activityId, PageHolder pageHolder){
        return ResultHolder.success(consumeActivityDecorator.getLookingById(storeId, activityId,pageHolder));
    }

    /**
     * @param storeId:     平台ID
     * @param userId:      用户ID
     * @param goodsId:商品ID
     * @return
     * @Author TanRuixiang
     * @Description 获取个体平台活动的商品信息
     * @Date 2020年09月28日 11:00
     **/
    @ApiOperation(value = "获取个体平台活动的商品信息")
    @GetMapping("/goods/detail/{goodsId}")
    public ResultHolder<StoreGoodsDetailVO> getGoodsDetail( @RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId, Long goodsId) {
        return ResultHolder.success(consumeActivityDecorator.getGoodsDetail(userId, goodsId));
    }

    @ApiOperation(value = "获取个体平台活动的商品的购买信息列表")
    @GetMapping("/goods/buy-infor/{goodsId}")
    public ResultHolder<IPage<OrderVO>> getBuyInfor(@RequestHeader(Constants.KEY_X_C3_STOREID) Long storeId, Long goodsId, PageHolder pageHolder) {
        return ResultHolder.success(consumeActivityDecorator.getBuyInfor(storeId, goodsId, pageHolder));
    }


    @ApiOperation(value = "判断该商品是否存在于活动中")
    @GetMapping("/goods/{goodsId}")
    public ResultHolder<Boolean> isExistGoodsInActivity(@RequestParam(value = "storeId") Long storeId, @RequestParam(value = "goodsId") Long goodsId) {
        return ResultHolder.success(consumeActivityDecorator.isExistGoodsInActivity(storeId, goodsId));
    }
}
